# -*- coding: UTF-8 -*-
import pymssql
import _mssql
import json
import requests
import sys
from datetime import date, datetime
from uuid import UUID
import decimal

def __default(obj):
    if isinstance(obj, datetime):
        return obj.strftime('%Y-%m-%d %H:%M:%S')
    elif isinstance(obj, date):
        return obj.strftime('%Y-%m-%d')
    elif isinstance(obj, UUID):
        return str(obj)
    elif isinstance(obj, decimal.Decimal):
        return float(obj)
    else:
        raise TypeError('%r is not JSON serializable' % obj)

reload(sys)   
sys.setdefaultencoding('utf8')


connlock=pymssql.connect(host='.',database='scm_main')
curlock=connlock.cursor()

curlock.execute('update ty_lock set id=1 where id=1')

conn=pymssql.connect(host='.',database='scm_main')
cur=conn.cursor(as_dict=True)

dwID=''
with open(sys.path[0]+'/dwid', 'r') as f:
        dwID=f.read()


cur.execute(u'select dep_serial bmid ,dep_name bmmc,dep_name fbmmc,' + dwID + ' dw from dt_dep ')
d=cur.fetchall()
payload = {'cards':json.dumps(d,default=__default,ensure_ascii=False)}
#print payload
r = requests.post("http://wx.nttengyuan.com:81/ty/sync/saveBM.do", data=payload)
rs=json.loads(r.text)
print u'同步部门信息',len(d),u'条'
   

cur.close()
conn.close()

connlock.commit()
curlock.close()
connlock.close()



